﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using OpenSoftStore.BusinessLogic.Common;
using OpenSoftStore.BusinessLogic.Admin;
using OpenSoftStore.Client.Common;
using OpenSoftStore.Orm.Model;
using System.Linq;
using OpenSoftStore.Orm.Static;

namespace OpenSoftStore.Client.UIL.ManageAdmin
{
    public partial class AdminList : UserControl
    {
        private static AdminList instance;
        private static AdminLogic bll = new AdminLogic();
        private static TreeLogic TreeBll;

        /// <summary>
        /// 返回一个该控件的实例。如果之前该控件已经被创建，直接返回已创建的控件。
        /// 此处采用单键模式对控件实例进行缓存，避免因界面切换重复创建和销毁对象。
        /// </summary>
        public static AdminList Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new AdminList();
                }
                //BindDataGrid(); // 每次返回该控件的实例前，都将对DataGridView控件的数据源进行重新绑定。
                return instance;
            }
        }

        /// <summary>
        /// 私有的控件实例化方法，创建实例只能通过该控件的Instance属性实现。
        /// </summary>
        private AdminList()
        {
            InitializeComponent();
            this.Dock = DockStyle.Fill;
            TreeBll = new TreeLogic();
            TreeBll.SetTreeNode(TreeBll.GetAdminTree(), tvAdmin);
            tvAdmin.ExpandAll();
            tvAdmin.SelectedNode = tvAdmin.Nodes[0];
            SPACommon.SetTreeStyle(tvAdmin);
            LoadGridView();
            SPACommon.SetDataGridStyle(dgvAdmin);

            btnTree_Add.Visible= SysPowerManager.IsHavePower(SysPowerManager.管理员工角色);
            btnTree_Update.Visible= SysPowerManager.IsHavePower(SysPowerManager.管理员工角色);
            btnTree_Delete.Visible = SysPowerManager.IsHavePower(SysPowerManager.管理员工角色);
            if (S_Admin.Name == "超级管理员") return;
            tsbAdd.Visible = SysPowerManager.IsHavePower(SysPowerManager.添加员工);
            tsbUpdate.Visible = SysPowerManager.IsHavePower(SysPowerManager.修改员工信息);
            tsbDelete.Visible = SysPowerManager.IsHavePower(SysPowerManager.删除员工);
        }

        public void LoadGridView()
        {
            dgvAdmin.AutoGenerateColumns = false;
            dgvAdmin.DataSource = bll.GetAdminView();
            lblCount.Text = dgvAdmin.RowCount.ToString();
        }

        private void tsbAdd_Click(object sender, EventArgs e)
        {
            AdminAddWindows adminAddWindow = new AdminAddWindows();
            adminAddWindow.SetTreeValue(tvAdmin.SelectedNode.Name, tvAdmin.SelectedNode.Text);
            adminAddWindow.ShowDialog();

        }

        private void tsbUpdate_Click(object sender, EventArgs e)
        {
            if (dgvAdmin.SelectedRows.Count > 0)
            {
                string id = dgvAdmin.SelectedRows[0].Cells["Id"].Value.ToString();
                AdminAddWindows adminAddWindow = new AdminAddWindows();
                adminAddWindow.GetAdmin(id);
                adminAddWindow.ShowDialog();
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (tvAdmin.SelectedNode == null) return;
            TreeNode tnParent = tvAdmin.SelectedNode;
            var tree = new T_Tree { ParentId = tnParent.Name };
            //tree.Name = tnParent.Text;
            var roleEditWindow = new RoleEditWindow();
            roleEditWindow.SetRole(tree);
            roleEditWindow.ShowDialog();
            if (roleEditWindow.DialogResult == DialogResult.Cancel) return;
        }

        public void AddTree(T_Tree tree)
        {
            TreeNode tnParent = tvAdmin.SelectedNode;
            var treeNode = new TreeNode { Text = tree.Name, Name = tree.Id, Tag = tree.Code };
            tnParent.Nodes.Add(treeNode);
            tvAdmin.SelectedNode = treeNode;
        }

        public void UpdateTree(T_Tree tree)
        {
            TreeNode tnParent = tvAdmin.SelectedNode;
            //var treeNode = new TreeNode { Text = tree.Name, Name = tree.Id, Tag = tree.Code };
            //tnParent.Nodes.Add(treeNode);
            tnParent.Name = tree.Id;
            tnParent.Text = tree.Name;
            tnParent.Tag = tree.Code;
            //tvAdmin.SelectedNode = treeNode;
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            if (tvAdmin.SelectedNode == null) return;
            if (tvAdmin.SelectedNode == null) return;
            TreeNode treeNode = tvAdmin.SelectedNode;
            var tree = new T_Tree { Id = treeNode.Name };
            var roleEditWindow = new RoleEditWindow();
            roleEditWindow.GetRole(tree.Id);
            roleEditWindow.ShowDialog();
            if (roleEditWindow.DialogResult == DialogResult.Cancel) return;

        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            TreeBll.Delete(tvAdmin);
        }

        private void tvAdmin_AfterLabelEdit(object sender, NodeLabelEditEventArgs e)
        {
            if (tvAdmin.SelectedNode == null) return;
            tvAdmin.SelectedNode.EndEdit(true);
            TreeBll.SaveTreeNode(tvAdmin, sender, e, "AdminTree");
        }

        private void tsbDelete_Click(object sender, EventArgs e)
        {
            if (dgvAdmin.SelectedRows.Count > 0)
            {
                if (MessageBox.Show("确定要删除吗？", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information) == DialogResult.OK)
                {
                    var id = dgvAdmin.SelectedRows[0].Cells["Id"].Value.ToString();
                    var admin = new T_Admin { Id = id };
                    AdminLogic adminBll = new AdminLogic();
                    adminBll.Delete(admin);
                    dgvAdmin.DataSource = bll.GetAdminView();
                }
            }

        }

        private void tsbGrant_Click(object sender, EventArgs e)
        {

        }

        private void dgvAdmin_DoubleClick(object sender, EventArgs e)
        {
            tsbUpdate_Click(null, null);
        }

        private void tvAdmin_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (tvAdmin.SelectedNode.Tag == null) return;
            dgvAdmin.DataSource = null;
            dgvAdmin.DataSource = bll.GetAdminViewFast(" where T_Tree.Code like '" + tvAdmin.SelectedNode.Tag.ToString() + "%' ");
            lblCount.Text = dgvAdmin.RowCount.ToString();
        }

        private void dgvAdmin_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex == -1) return;
            tsbUpdate_Click(null, null);
        }
    }
}
